import 'dart:convert' as convert;

import 'package:flutter/cupertino.dart';
import 'package:intl/intl.dart';
import 'package:flutter_syt/application.dart';
import 'package:flutter_syt/datas/number_extension.dart';

class ThirdUserInfo {
  late String openid;
  late String nickname;
  late String headimgurl;
  late int sex;
  ThirdUserInfo(Map<String, dynamic> json) {
    openid = json["openid"] as String;
    nickname = json["nickname"] as String;
    headimgurl = json["headimgurl"] as String;
    sex = json["sex"] as int;
  }
  Map<String, dynamic> json() {
    return {
      "openid": openid,
      "nickname": nickname,
      "headimgurl": headimgurl,
      "sex": sex,
    };
  }
}

class UserCert {
  late String name_zh;
  late String name_en;
  late String certificate_type;
  late String license_number;
  String get name {
    var local =
        appContext.storage.getString("storyLocal") ?? Intl.getCurrentLocale();
    if (local.contains('zh')) {
      return name_zh;
    } else {
      return name_en;
    }
  }

  UserCert(Map<String, dynamic> json) {
    name_zh = "${json["name_zh"]}";
    name_en = "${json["name_en"]}";
    certificate_type = "${json["certificate_type"]}";
    license_number = "${json["license_number"]}";
  }
  Map<String, dynamic> get json {
    return Map<String, dynamic>()
      ..["name_zh"] = name_zh
      ..["name_en"] = name_en
      ..["certificate_type"] = certificate_type
      ..["license_number"] = license_number;
  }
}

class UserRole {
  late String id;
  late String name_en;
  late String name_zh;
  String get name {
    var local =
        appContext.storage.getString("storyLocal") ?? Intl.getCurrentLocale();
    if (local.contains('zh')) {
      return name_zh;
    } else {
      return name_en;
    }
  }

  UserRole(Map<String, dynamic> json) {
    id = "${json["id"]}";
    name_en = "${json["name_en"]}";
    name_zh = "${json["name_zh"]}";
  }
  Map<String, dynamic> get json {
    return Map<String, dynamic>()
      ..["id"] = id
      ..["name_zh"] = name_zh
      ..["name_en"] = name_en;
  }
}

class UserInfo extends ChangeNotifier {
  static UserInfo me = UserInfo._();

  static load() {
    var jsonString = appContext.storage.getString("user_info") ?? "";
    try {
      if (jsonString.isNotEmpty) {
        Map<String, dynamic> map = convert.json.decode(jsonString);
        update(map);
      }
    } catch (e) {}
  }

  static update(Map<String, dynamic> json) {
    if (json["token"] != null) {
      me.token = json["token"] as String;
    }
    me.nickname = "${json["nickname"]}";
    me.avatar = "${json["avatar"]}";
    me.sex = parseInt(json["sex"]);
    me.birthday = json["birthday"] as String;
    me.roles = (json["roles"] as List).map((e) => UserRole(e)).toList();
    me.phone_number = "${json["phone_number"]}";
    me.area_code = "${json["area_code"]}";
    me.role_name = "${json["role_name"]}";
    me.registerType = "${json["register_type"]}";
    me.email = json["email"] as String?;
    me.serial_number = json['serial_number'] ?? 0;
    try {
      me.id = json["id"] as int;
    } catch (e) {}

    try {
      me.cert = UserCert(json["cert"]);
    } catch (e) {}

    var value = convert.json.encode(me.jsonValue);
    appContext.storage.setString("user_info", value);

    me.notifyListeners();
    return me;
  }

  static void save() {
    var value = convert.json.encode(me.jsonValue);
    appContext.storage.setString("user_info", value);
    me.notifyListeners();
  }

  static clean() {
    me.initValues();
    // WebView()
    // appContext.webController?.clearCache();
    appContext.storage.removeValue("user_info");
    me.notifyListeners();
  }

  static justNotify() {
    me.notifyListeners();
  }

  late String token;
  late String nickname;
  late String avatar;
  late int sex;
  late String birthday;
  late List<UserRole> roles;
  late String phone_number;
  late String area_code;
  late String role_name;
  late int serial_number;
  late int id;
  late UserCert cert;
  String? email;

  String? registerType;
  String get name {
    return me.cert.name;
  }

  late bool authed;

  bool get isLogin {
    return token != "";
  }

  UserInfo._() {
    initValues();
  }
  void initValues() {
    this.token = "";
    this.nickname = "";
    this.avatar = "";
    this.sex = 0;
    this.birthday = "";
    this.roles = [];
    this.phone_number = "";
    this.area_code = "";
    this.role_name = "";
    this.id = 0;
    this.serial_number = 0;
    // this.email;
    this.authed = false;
    cert = UserCert({});
  }

  UserInfo(Map<String, dynamic> json) {
    this.token = json["token"] as String;
    this.nickname = json["nickname"] as String;
    this.avatar = json["avatar"] as String;
    this.sex = json["sex"] as int;
    this.birthday = json["birthday"] as String;
    this.roles = (json["roles"] as List).map((e) => UserRole(e)).toList();
    this.phone_number = json["phone_number"] as String;
    this.email = json["email"] as String?;
    this.registerType = json['registe_type'] as String?;
    this.area_code = json["area_code"] as String;
    this.role_name = "${json["role_name"]}";
    this.id = json["id"] as int;
    this.serial_number = json['serial_number'] as int;
    try {
      this.authed = json["authed"] as bool;
    } catch (e) {
      this.authed = false;
    }
  }
  Map<String, dynamic> get jsonValue {
    var result = Map<String, dynamic>();
    result["token"] = this.token;
    result["nickname"] = this.nickname;
    result["avatar"] = this.avatar;
    result["sex"] = this.sex;
    result["birthday"] = this.birthday;
    result["roles"] = this.roles.map((e) => e.json).toList();
    result["phone_number"] = this.phone_number;
    result["area_code"] = this.area_code;
    result["role_name"] = this.role_name;
    result["area_code"] = this.area_code;
    result["email"] = this.email;
    result["register_type"] = this.registerType;
    result["cert"] = cert.json;
    result["id"] = this.id;
    result["name"] = name;
    result['serial_number'] = this.serial_number;
    result["authed"] = this.authed;
    return result;
  }
}
